package com.helloandroid.paso.ui.mainmenu;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.Gallery;
import android.widget.ImageButton;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;

import com.helloandroid.paso.R;
import com.helloandroid.paso.ui.concert.Concert;
import com.helloandroid.paso.ui.crewlist.CrewListView;
import com.helloandroid.paso.ui.diszko.diszko_ui_main;
import com.helloandroid.paso.ui.mediaplayer.MPlayer;
import com.helloandroid.paso.ui.multimedia.Multimedia;
import com.helloandroid.paso.ui.news.News;
import com.helloandroid.paso.ui.social.Social;

public class MainMenu extends Activity {
	private Button mNewsButton, mConcertsButton, mMembersButton, mDiscoButton,
			mMultimButton, mSocialButton;
	private ViewFlipper mViewFlipper;
	private int mActualPageId = 0;
	private diszko_ui_main mDiscografia;
	private Concert mConcert;
	private News mNews;
	private Social mSocial;
	private Multimedia mMultimedia = new Multimedia();
	private CrewListView mCrewlist;
	private MPlayer mplayer;

	/** Called when the activity is first created. */
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		setLayout();
		setLayoutListeners();
	}

	private void setLayout() {
		requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView(R.layout.mainmenu);

		mViewFlipper = (ViewFlipper) findViewById(R.id.flipper);
		mNewsButton = (Button) findViewById(R.id.buttonNews);
		mConcertsButton = (Button) findViewById(R.id.buttonConcerts);
		mMembersButton = (Button) findViewById(R.id.buttonMembers);
		mDiscoButton = (Button) findViewById(R.id.buttonDiscography);
		mMultimButton = (Button) findViewById(R.id.buttonMultimedia);
		mSocialButton = (Button) findViewById(R.id.buttonSocial);
		mViewFlipper = (ViewFlipper) findViewById(R.id.viewFlipper1);

		mDiscografia = new diszko_ui_main();
		mConcert = new Concert();
		mNews = new News();
		mSocial = new Social();
		// mMultimedia = new Multimedia(getApplicationContext());
		mCrewlist = new CrewListView();
		mplayer = new MPlayer();

		mplayer.InitMPlayer(this,
				(TextView) findViewById(R.id.mp_trackInfoTextView),
				(ImageButton) findViewById(R.id.mp_playPauseButton),
				(ImageButton) findViewById(R.id.mp_nextButton),
				(ImageButton) findViewById(R.id.mp_trackListButton),
				(ProgressBar) findViewById(R.id.mp_progressBar),  
				R.layout.mp_trackdialog,
				R.drawable.mp_pausebutton, R.drawable.mp_playbutton,
				R.layout.mp_dialog_listitem,
				R.id.trackDialogListView,
				R.id.trackTitleText);
		mplayer.setPlaylist(getResources().getStringArray(R.array.mp3_URLs));

		mNews.getNews((WebView) findViewById(R.id.wView),
				getText(R.string.newsURL).toString(),
				(ProgressBar) findViewById(R.id.wVProgress));
		setPage(0);
		makePopup().show();
	}

	/**
	 * Setting the actual page
	 * 
	 * @param nextId
	 * 
	 */
	private void setPage(int nextId) {
		if (nextId < mActualPageId) {
			for (int i = 0; i < mActualPageId - nextId; i++) {
				mViewFlipper.setInAnimation(inFromRightAnimation());
				mViewFlipper.setOutAnimation(outToLeftAnimation());
				mViewFlipper.showPrevious();
			}
		} else if (nextId > mActualPageId) {
			for (int i = 0; i < nextId - mActualPageId; i++) {
				mViewFlipper.setInAnimation(inFromLeftAnimation());
				mViewFlipper.setOutAnimation(outToRightAnimation());
				mViewFlipper.showNext();
			}
		}
		mActualPageId = nextId;
	}

	private void setLayoutListeners() {

		mNewsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setPage(0);
				mNews.getNews((WebView) findViewById(R.id.wView),
						getText(R.string.newsURL).toString(),
						(ProgressBar) findViewById(R.id.wVProgress));
			}
		});

		mConcertsButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setPage(1);
				mConcert.getConcertsView(getApplicationContext(),
						(WebView) findViewById(R.id.concert_webview),
						(ProgressBar) findViewById(R.id.pbLoading));
			}
		});

		mMembersButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setPage(2);
				mCrewlist
						.getCrewList(
								getResources().getStringArray(
										R.array.members_array),
								getResources().getStringArray(
										R.array.crew_array),
								getResources().getStringArray(
										R.array.prevmembers_array),
								getResources().getStringArray(
										R.array.memberslink_array),
								(ListView) findViewById(R.id.ListOfMembers),
								(Button) findViewById(R.id.MembersButton),
								(Button) findViewById(R.id.CrewButton),
								(Button) findViewById(R.id.PrevMembersButton),
								R.layout.list_custom_text,
								R.id.list_content,
								(WebView) findViewById(R.id.detailWebview),
								getApplicationContext(),
								(RelativeLayout) findViewById(R.id.CrewList_ListHolder),
								(RelativeLayout) findViewById(R.id.WebWiewLayout),
								(Button) findViewById(R.id.BackButton));
			}
		});

		mDiscoButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setPage(3);
				mDiscografia.getDiszko(
						(WebView) findViewById(R.id.diszko_ui_screen_webview),
						(Gallery) findViewById(R.id.diszko_ui_screen_galery),
						getResources(), R.array.url_addres,
						getApplicationContext());

			}
		});

		mMultimButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setPage(4);
				mMultimedia
						.getMultimedia(
								(ListView) findViewById(R.id.multimedia_listView1),
								(Button) findViewById(R.id.multimedia_button_gallery),
								(Button) findViewById(R.id.multimedia_button_video),
								getApplicationContext(),
								(ProgressBar) findViewById(R.id.multimedia_progressBar1),
								(WebView) findViewById(R.id.multimedia_webview),
								(Gallery) findViewById(R.id.multimedia_galery),
								getResources(), R.array.url_addres);
			}
		});

		mSocialButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				setPage(5);
				mSocial.InitSocial(
						(WebView) findViewById(R.id.webkitWebView1),
						(ProgressBar) findViewById(R.id.progressBar1),
						(ImageButton) findViewById(R.id.facebookButton),
						(ImageButton) findViewById(R.id.myspaceButton),
						(ImageButton) findViewById(R.id.iwiwButton),
						"http://www.facebook.com/pannoniaallstars",
						"http://www.myspace.com/paso",
						"http://iwiw.hu/i/klub/zene/Pannonia-Allstars-Ska-Orchestra-PASO-1011123?cID=1011123");

			}
		});
	}

	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		if ((keyCode == KeyEvent.KEYCODE_BACK)) {
			Log.d(this.getClass().getName(), "back button pressed");
			setPage(2);
			mCrewlist.getCrewList(
					getResources().getStringArray(R.array.members_array),
					getResources().getStringArray(R.array.crew_array),
					getResources().getStringArray(R.array.prevmembers_array),
					getResources().getStringArray(R.array.memberslink_array),
					(ListView) findViewById(R.id.ListOfMembers),
					(Button) findViewById(R.id.MembersButton),
					(Button) findViewById(R.id.CrewButton),
					(Button) findViewById(R.id.PrevMembersButton),
					R.layout.list_custom_text, R.id.list_content,
					(WebView) findViewById(R.id.detailWebview),
					getApplicationContext(),
					(RelativeLayout) findViewById(R.id.CrewList_ListHolder),
					(RelativeLayout) findViewById(R.id.WebWiewLayout),
					(Button) findViewById(R.id.BackButton));
		}
		return super.onKeyDown(keyCode, event);
	}

	/**
	 * Animation for ViewFlipper
	 * 
	 * @return
	 */
	public static Animation inFromRightAnimation() {
		Animation inFromRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromRight.setDuration(350);
		inFromRight.setInterpolator(new AccelerateInterpolator());
		return inFromRight;
	}

	public static Animation outToLeftAnimation() {
		Animation outtoLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoLeft.setDuration(350);
		outtoLeft.setInterpolator(new AccelerateInterpolator());
		return outtoLeft;
	}

	public static Animation inFromLeftAnimation() {
		Animation inFromLeft = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, -1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		inFromLeft.setDuration(350);
		inFromLeft.setInterpolator(new AccelerateInterpolator());
		return inFromLeft;
	}

	public static Animation outToRightAnimation() {
		Animation outtoRight = new TranslateAnimation(
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, +1.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f,
				Animation.RELATIVE_TO_PARENT, 0.0f);
		outtoRight.setDuration(350);
		outtoRight.setInterpolator(new AccelerateInterpolator());
		return outtoRight;
	}

	public Dialog makePopup() {

		final Dialog dialog = new Dialog(MainMenu.this);
		dialog.setContentView(R.layout.concert_popup);
		dialog.setTitle(getString(R.string.newConcertsLabel));
		dialog.setCancelable(true);
		// there are a lot of settings, for dialog, check them all out!

		// set up text
		WebView wv = (WebView) dialog.findViewById(R.id.popup_concert_webview);
		final ProgressBar pb = (ProgressBar) dialog
				.findViewById(R.id.pbPopupLoading);

		wv.setWebViewClient(new WebViewClient() {
			@Override
			public void onReceivedError(WebView view, int errorCode,
					String description, String failingUrl) {
				pb.setVisibility(View.INVISIBLE);

				Toast.makeText(getApplicationContext(),
						"No internet connection", Toast.LENGTH_LONG).show();

			}

			public void onPageFinished(WebView view, String url) {
				pb.setVisibility(View.GONE);
			}

			public void onPageStarted(WebView view, String url, Bitmap favicon) {
				pb.setVisibility(View.VISIBLE);
			}

			@Override
			public boolean shouldOverrideUrlLoading(WebView view, String url) {

				view.loadUrl(url);
				return true;
			}
		});

		wv.loadUrl(getString(R.string.newConcertsUrl));

		// set up button
		Button button = (Button) dialog.findViewById(R.id.closeConcertPopup);
		button.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				dialog.cancel();
			}
		});
		// now that the dialog is set up, it's time to show it
		return dialog;
	}
}
